version 8.2
capture clear
capture log close
set more off
set mem 510m
set mat 800

use "data_for_regression_opinion surveys_freerider.dta"


global list_controls fshortdum1 fshortdum2 fshortdum3 fshortdum4 fshortdum5 female origin3_dum1 origin3_dum2 origin3_dum3 origin3_dum4 origin3_dum5 class_clean_dum1 class_clean_dum2 class_clean_dum3 class_clean_dum4
global list_controls_diversity number_female_T div_group_field_T div_group_origin4_T number_third_fourth_T

gen delta_quiz=quiz3-quiz1

keep term id C_T_9_byoth_good C_T_10_byoth_good C_T_11_byoth_good ///
C_T_9_byothers C_T_10_byothers C_T_11_byothers ///
C_T_12_me C_T_mycontrib C_T_mean_mycontrib ///
posting_quiz1_quiz2 assign_appointment assign_proactive freerider_reported_assign ///
quiz1 delta_quiz assignment mean_group_quiz1 sd_group_quiz1 mean_class_quiz1 ///
email_T_dummy_group monitoring_T bad_50_T good_50_T term_group_T position_number_T ///
$list_controls $list_controls_diversity Fall201* ///
french_name english_name smile

* freerider_reported delta_quiz 

ren C_T_9_byoth_good final_9_byoth_good
ren C_T_10_byoth_good final_10_byoth_good
ren C_T_11_byoth_good final_11_byoth_good

ren C_T_9_byothers final_9_byothers
ren C_T_10_byothers final_10_byothers
ren C_T_11_byothers final_11_byothers


ren C_T_12_me final_12_me
ren C_T_mycontrib final_mycontrib
ren C_T_mean_mycontrib final_mean_mycontrib

ren posting_quiz1_quiz2 posting 
ren assign_appointment appointment
ren assign_proactive proactive

ren freerider_reported_assign freerider_reported

ren quiz1 quiz_bef

ren assignment grade_teamwork

ren mean_group_quiz1 mean_group_quiz
ren sd_group_quiz1 sd_group_quiz
ren mean_class_quiz1 mean_class_quiz
 
*interactions: 
ren email_T_dummy_group email_dummy_group
ren monitoring_T monitoring


*median:
ren bad_50_T bad_50
ren good_50_T good_50

ren term_group_T term_group_T_P

 
****diversity:
ren number_female_T number_female
ren div_group_field_T div_group_field
ren div_group_origin4_T div_group_origin4
ren number_third_fourth_T number_third_fourth


ren position_number_T position_number
gen teamwork=1


save "data_teamwork.dta", replace

clear


use "data_for_regression_opinion surveys_freerider.dta"

***for a referee's comment:
*students in the treatment group for Task 1 and in the control group for Task 2
*They know that peer review was conducted for Task 1.
*There is no peer review for them at the beginning of Task 2
*yet they may believe there will be peer review in Task 2 considering their experience in Task 1
*We isolate them to drop them form the analysis
*Note: we only run this in the presentation teamwork since they are contaminated in the presentation teamwork, not the statistical teamwork

*browse id monitoring_P monitoring_T
gen contaminated_control=1 if monitoring_P==0&monitoring_T==1
replace contaminated_control=0 if contaminated_control==.

gen delta_quiz=quiz5-quiz3

global list_controls fshortdum1 fshortdum2 fshortdum3 fshortdum4 fshortdum5 female origin3_dum1 origin3_dum2 origin3_dum3 origin3_dum4 origin3_dum5 class_clean_dum1 class_clean_dum2 class_clean_dum3 class_clean_dum4
global list_controls_diversity number_female_P div_group_field_P div_group_origin4_P number_third_fourth_P


keep term id C_P_9_byoth_good C_P_10_byoth_good C_P_11_byoth_good ///
C_P_9_byothers C_P_10_byothers C_P_11_byothers ///
C_P_12_me C_P_mycontrib C_P_mean_mycontrib ///
posting_quiz3_quiz5 presentation_appointment presentation_proactive freerider_reported_pres ///
quiz3 delta_quiz presentation mean_group_quiz3 sd_group_quiz3 mean_class_quiz3 ///
email_P_dummy_group monitoring_P bad_50_P good_50_P term_group_P position_number_P ///
$list_controls $list_controls_diversity Fall201* ///
french_name english_name smile ///
contaminated_control 

* freerider_reported delta_quiz 

ren C_P_9_byoth_good final_9_byoth_good
ren C_P_10_byoth_good final_10_byoth_good
ren C_P_11_byoth_good final_11_byoth_good

ren C_P_9_byothers final_9_byothers
ren C_P_10_byothers final_10_byothers
ren C_P_11_byothers final_11_byothers


ren C_P_12_me final_12_me
ren C_P_mycontrib final_mycontrib
ren C_P_mean_mycontrib final_mean_mycontrib

ren posting_quiz3_quiz5 posting 
ren presentation_appointment appointment
ren presentation_proactive proactive

ren freerider_reported_pres freerider_reported

ren quiz3 quiz_bef

ren presentation grade_teamwork

ren mean_group_quiz3 mean_group_quiz
ren sd_group_quiz3 sd_group_quiz
ren mean_class_quiz3 mean_class_quiz
 
*interactions: 
ren email_P_dummy_group email_dummy_group
ren monitoring_P monitoring


*median:
ren bad_50_P bad_50
ren good_50_P good_50

ren term_group_P term_group_T_P

 
****diversity:
ren number_female_P number_female
ren div_group_field_P div_group_field
ren div_group_origin4_P div_group_origin4
ren number_third_fourth_P number_third_fourth


ren position_number_P position_number
gen teamwork=0


save "data_presentation.dta", replace
*****now add teamwork and presentation together:
append using "data_teamwork.dta"



*Note: there is no contaimnated control in the statistical teamwork 
replace contaminated_control=0 if teamwork==1
*browse id teamwork contaminated_control





***Labels:


label variable email_dummy_group "Email treatment group"


label variable teamwork "Statistical Teamwork"


*generate an index of effort:
gen effort_final=1 if final_9_byoth_good==1&final_10_byoth_good==1&final_11_byoth_good==1
replace effort_final=0 if (final_9_byoth_good==0|final_10_byoth_good==0|final_11_byoth_good==0)

****generate the mean, not the min of effort:
egen effort_mean=rowmean(final_9_byothers final_10_byothers final_11_byothers)
*browse effort_mean

*****size of group
gen one=1
egen size_group=sum(one), by(term_group_T_P)
gen group_of_3=1 if size_group==3
replace group_of_3=0 if size_group~=3

****diversity:
label variable div_group_field "Number of different fields in team"
label variable div_group_origin4 "Number of different ethnic groups in team"

label variable number_female "Number female in team"
label variable number_third_fourth "Number of third-fourth year students in team"


label variable group_of_3 "Group of three students"

***labels:
label variable posting "Postings"
label variable freerider_reported "Free-rider denounced"

label variable final_9_byoth_good "Fair share?" 
label variable final_10_byoth_good "On time?" 
label variable final_11_byoth_good "Deadlines?" 

label variable final_12_me "Enthusiastic?" 

label variable effort_final "Effort"



*outcomes:
label variable effort_final "Effort"
label variable effort_mean "Effort: mean"
label variable posting  "Postings"
label variable freerider_reported "Extreme shirker reported"
label variable final_12_me "Enthusiasm"
label variable delta_quiz "Increase in grade"


****building variables for team productivity:
sort term_group_T_P
*sort grade_teamwork	

***averages per group:
local list_variables grade_teamwork quiz_bef sd_group_quiz fshortdum2 fshortdum3 fshortdum4 fshortdum5 female origin3_dum1 origin3_dum2 origin3_dum3 origin3_dum5 class_clean_dum1 class_clean_dum3 class_clean_dum4 number_female div_group_field div_group_origin4 number_third_fourth group_of_3
foreach var of local list_variables{
	egen `var'_t=mean(`var'), by(term_group_T_P)
	}

local list_variables2 monitoring email_dummy_group
foreach var of local list_variables2{
	egen `var'_t=max(`var'), by(term_group_T_P)
	}

***interaction terms:
gen mon_mean_group_quiz_t=monitoring_t*quiz_bef_t

***control variables:
global list_controls_t fshortdum2_t fshortdum3_t fshortdum4_t fshortdum5_t female_t origin3_dum1_t origin3_dum2_t origin3_dum3_t origin3_dum5_t class_clean_dum1_t class_clean_dum3_t class_clean_dum4_t

label variable monitoring_t "Peer review"

***we build an indicator to keep only one observation per group, without deleting the whole sample
by term_group_T_P: gen group_obs = 1 if _n == 1 



/*OLD was here, it should be below: see NEW
***interaction wtih individual ability:
local list_variables monitoring mean_group_quiz mon_mean_group_quiz
foreach var of local list_variables{	
	gen quiz_bef_`var'=quiz_bef*`var'
	}


	
label variable quiz_bef "Baseline Ability"
label variable quiz_bef_monitoring "Peer Review * Baseline Ability"
label variable quiz_bef_mean_group_quiz "Team Ability * Baseline Ability"
label variable quiz_bef_mon_mean_group_quiz "Peer Review * Team Ability * Baseline Ability"
	
*/	
	
sum mean_group_quiz
count if mean_group_quiz==0

***change of reference:
*drop mon_mean_group_quiz email_mean_group_quiz
ren mean_group_quiz mean_group_quiz_notst
*1.574076
gen mean_group_quiz=(mean_group_quiz_notst)/.627193
sum mean_group_quiz

	
**Other interactions:	
gen mon_mean_group_quiz=monitoring*mean_group_quiz





******NEW:


***interaction with individual ability:
local list_variables monitoring mean_group_quiz mon_mean_group_quiz
foreach var of local list_variables{	
	gen quiz_bef_`var'=quiz_bef*`var'
	}


	
label variable quiz_bef "Baseline Ability"
label variable quiz_bef_monitoring "Peer Review * Baseline Ability"
label variable quiz_bef_mean_group_quiz "Team Ability * Baseline Ability"
label variable quiz_bef_mon_mean_group_quiz "Peer Review * Team Ability * Baseline Ability"






******END



*explanatory variables:
label variable mean_group_quiz "Team Ability"
label variable monitoring "Peer Review"
label variable mon_mean_group_quiz "Peer Review * Team Ability"


label variable sd_group_quiz "Variance Group Score"


sum mean_group_quiz



save "data_mcgill_final.dta", replace


exit
